﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace IA_Project
{
    public partial class Project : Form
    {
        public Project()
        {
            InitializeComponent();
        }

        Image<Gray, byte> img1, img2, img3;

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                Image<Gray, byte> temp_load = new Image<Gray, byte>(openFileDialog1.FileName);
                imageBox1.Image = temp_load;
                img1 = temp_load.Clone();
            }
        }

        private void Hist_eq()
        {
            img2 = img1.Clone();
            int[] hist = new int[256];
            int[] cdf = new int[256];
            int[] eq = new int[256];

            for (int i = 0; i < 256; i++)
            {
                hist[i] = 0;
                cdf[i] = 0;
                eq[i] = 0;
            }

            for (int i = 0; i < img1.Height; i++)
            {
                for (int j = 0; j < img1.Width; j++)
                {
                    int val = (int)(img1[i, j].Intensity);
                    hist[val] = hist[val] + 1;    
                }
            }


            cdf[0] = hist[0];
            for (int i = 1; i < 256; i++)
            {
                cdf[i] = cdf[i - 1] + hist[i];
            }


            for (int i = 0; i < img1.Height; i++)
            {
                for (int j = 0; j < img1.Width; j++)
                {
                    int val = (int)(img1[i, j].Intensity);
                    eq[val] = (int)(((cdf[val] - cdf[0]) * 255.0) / ((img1.Width * img1.Height) - cdf[0]));
                    Gray temp = new Gray();
                    temp.Intensity = eq[val];
                    img2[i, j] = temp;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Hist_eq();
            imageBox2.Image = img2;
        }

        private void Sobel()
        {
            img3 = img2.Clone();
            int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
            int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
            int[,] G = new int[img2.Height, img2.Width];

            for (int i = 1; i < img2.Height - 1; i++)
            {
                for (int j = 1; j < img2.Width - 1; j++)  
                {
                    float new_x = 0, new_y = 0;
                    float c;
                    for (int hw = 0; hw < 3; hw++)
                    {
                        for (int wi = 0; wi < 3; wi++)
                        {
                            c = (float)(img2[i, j].Intensity);
                            new_x = new_x + gx[hw, wi] * c;
                            new_y = new_y + gy[hw, wi] * c;
                        }
                    }

                    G[i, j] = (int)(Math.Sqrt(Math.Pow(new_x,2) + Math.Pow(new_y,2)));   
                }
            }

            for (int i = 1; i < img2.Height - 1; i++)
            {
                for (int j = 1; j < img2.Width - 1; j++)
                {
                    Gray temp = new Gray();
                    temp.Intensity = G[i, j];
                    img3[i, j] = temp;
                }
            }
            
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //img3 = img2.Clone();
            //CvInvoke.cvCanny(img2, img3, 150, 60, 3);
            Sobel();
            imageBox3.Image = img3 ;
        }
    }
}
